using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.NotificationServices;

namespace Subscription_Management
{
    public partial class frmSubscriptionManagement : Form
    {
        public frmSubscriptionManagement()
        {
            InitializeComponent();
        }

        private void btnAddSubscriber_Click(object sender, EventArgs e)
        {
            // Create the NSInstance object.
            NSInstance hceInstance = new NSInstance("HCENotificationService");

            // Create the Subscriber object.

            Subscriber newSubscriber = new Subscriber(hceInstance);
            newSubscriber.Enabled = true;
            newSubscriber.SubscriberId = txtSubscriberID.Text;
            newSubscriber.Add();

            // Create the SubscriberDevice object.
            SubscriberDevice newDevice = new SubscriberDevice(hceInstance);

            // Set the properties that describe the subscriber device record.
            newDevice.SubscriberId = newSubscriber.SubscriberId;
            newDevice.DeviceAddress = "";
            newDevice.DeviceName = "CSA Device";
            newDevice.DeviceTypeName = "CSA";
            newDevice.DeliveryChannelName = "HCEChannel";

            // Add Device.           
            newDevice.Add();

            UpdateCmbSubscribers();

            MessageBox.Show("A new subscriber was sucessfully added to the Change Notification Service");

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            UpdateCmbSubscribers();
        }

        private void UpdateCmbSubscribers()
        {
            NSInstance hceInstance = new NSInstance("HCENotificationService");
            SubscriberEnumeration subscriberList = new SubscriberEnumeration(hceInstance);
            cmbSubscribers.Items.Clear();
            foreach (Subscriber item in subscriberList)
            {
                cmbSubscribers.Items.Add(item.SubscriberId);
            }
            cmbSubscribers.Refresh();
        }

        private void btnAddSubscription_Click(object sender, EventArgs e)
        {
            // Create the NSInstance object.
            NSInstance hceInstance = new NSInstance("HCENotificationService");

            // Create the NSApplication object.
            NSApplication hsdApp =
                new NSApplication(hceInstance, "ServiceProviderChangeNotification");

            // Create the Subscription object.
            Subscription newSubscription =
                new Subscription(hsdApp, "ServiceProviderSubscription");


            // Set the properties that describe the subscription record.
            newSubscription.Enabled = true;
            newSubscription.SubscriberId = cmbSubscribers.SelectedItem.ToString() ;

            // Set the subscription data fields (as defined in the ADF),
            // using the indexer to set fields by field name.
            newSubscription["DeviceName"] = "CSA Device";
            newSubscription["SubscriberLocale"] = "en-NZ";
            newSubscription["ServiceProviderID"] = txtProviderID.Text;

            // Add the subscription to the database.
            newSubscription.Add();

            MessageBox.Show("A new subscription was sucessfully added to the Change Notification Service");
        }

        private void btnRemoveSubscriber_Click(object sender, EventArgs e)
        {
            // Create the NSInstance object.
            NSInstance hceInstance = new NSInstance("HCENotificationService");

            // Create the Subscriber object.
            Subscriber newSubscriber = new Subscriber(hceInstance);

            newSubscriber.SubscriberId = cmbSubscribers.SelectedItem.ToString();
            newSubscriber.Delete();

            UpdateCmbSubscribers();
        }

    }
}